import { createApp } from 'vue'
import App from './App.vue'
import router from './router'

let unlisten: any = null

if ((window as any).__POWERED_BY_WUJIE__) {
    ;(window as any).__WUJIE_MOUNT = () => {
        const app = createApp(App)
        app.use(router)
        app.mount('#app')

        ;(window as any).$wujie?.bus.$off('main-route-change')
        // 监听主应用路由变化事件
        unlisten = (window as any).$wujie?.bus.$on(
            'main-route-change',
            (path: string) => {
                console.log(
                    'main-route-change: ->>>>sub---proa====',
                    path,
                    '===',
                    router.currentRoute.value.path
                )
                // 避免重复跳转
                if ('/proa' + router.currentRoute.value.path !== path) {
                    const { 1: pa, 2: p } = path.split('/')
                    if (pa === 'proa') {
                        console.log('p-proa: ', p)
                        router.push({ path: '/' + p })
                    }
                }
            }
        )
    }
    ;(window as any).__WUJIE_UNMOUNT = () => {
        // 清理事件监听
        if (unlisten) unlisten()
    }
} else {
    // 独立运行
    // createApp(App).use(createRouter({ /* 路由配置 */ })).mount('#app')
    const app = createApp(App)
    app.use(router)
    app.mount('#app')
}
